package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.TaskDao;
import com.rstrulak.model.Task;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("taskBeanList")
@Scope("request")
public class TaskBeanList implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1387923867802583944L;

	@Autowired
	private TaskDao taskDao;

	private List<Task> taskList;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
		getTaskListFromDb();
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}

	private void getTaskListFromDb() {
		taskList = taskDao.getTaskListByUser(JSFUtils.getLoggedUser());
	}

	public void deleteTask(Task task) {
		taskDao.delete(task);
		getTaskListFromDb();
	}

	public void goToCreateTaskSite() throws ServletException, IOException {
		JSFUtils.redirect("/protected/newTaskWithoutProject.xhtml");
	}

	public void showTaskDetails(Long taskId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.TASK_ID, taskId);
		JSFUtils.redirect("/protected/taskDetails.xhtml");
	}

	public void showProjectDetails(Long projectId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.PROJECT_ID, projectId);
		JSFUtils.redirect("/protected/projectDetails.xhtml");
	}

	public void showContextDetails(Long contextId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.CONTEXT_ID, contextId);
		JSFUtils.redirect("/protected/contextDetails.xhtml");
	}

}
